#include "StdAfx.h"
#include <iostream>
#include <fstream>
#include <sstream>

#include "OpenGLInputLoader.h"
#include "OpenGLTriangle.h"
#include "OpenGLLine.h"

bool OpenGLInputLoader::LoadPrimitives(const string& strFileName, vector<COpenGLPrimitivePtr>& vTriangles, vector<COpenGLPrimitivePtr>& vLines)
{
	fstream inputFileStream(strFileName, ios_base::in);

	// read triangles
	int iNumTriangles = 0;
	inputFileStream >> iNumTriangles;
	for (int i = 0; i < iNumTriangles; ++i)
	{
		COpenGLPrimitivePtr spTriangle(new COpenGLTriangle);

		for (int j = 0; j < 3; ++j)
		{
			CVertex& v = spTriangle->GetVertex(j);

			inputFileStream >> v.worldPosition[0];
			inputFileStream >> v.worldPosition[1];
			v.worldPosition[2] = 0;
			v.worldPosition[3] = 1;

			inputFileStream >> v.color[0];
			inputFileStream >> v.color[1];
			inputFileStream >> v.color[2];
			inputFileStream >> v.color[3];
		}
		vTriangles.push_back(spTriangle);
	}

	// read lines
	int iNumLines = 0;
	inputFileStream >> iNumLines;
	for (int i = 0; i < iNumLines; ++i)
	{
		COpenGLPrimitivePtr spLine(new COpenGLLine);

		for (int j = 0; j < 2; ++j)
		{
			CVertex& v = spLine->GetVertex(j);

			inputFileStream >> v.worldPosition[0];
			inputFileStream >> v.worldPosition[1];
			v.worldPosition[2] = 0;
			v.worldPosition[3] = 1;

			inputFileStream >> v.color[0];
			inputFileStream >> v.color[1];
			inputFileStream >> v.color[2];
			inputFileStream >> v.color[3];
		}
		vLines.push_back(spLine);
	}

	inputFileStream.close();

	return true;
}
